﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Linq_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Label1_Bind();
        if (!IsPostBack)
        {
            GridView1_Bind();
        }
    }

    private void GridView1_Bind()
    {
        DBtestDataContext db = new DBtestDataContext();
        var q1 = from e in db.Employees
                               select e;

        //  Where
        var q2 = db.Employees.Where(e => e.ID == 2);
        var q3 = db.Employees.Where(e => e.ID > 1).Where(e => e.Employee1 == "Jerry");

        //  First()  , like top(1)
        Employee employee = db.Employees.First(e => e.ID > 0);
        Label1.Text = employee.Employee1;

        //  Select
        var q4 = from e in db.Employees
                 //select e.Employee1 ;
                 select new 
                 { 
                     e.Employee1, 
                     e.ID,
                     EmployeeName = "Mr. " + e.Employee1,
                     twiceID = e.ID * 2,

                     // case when condition then else
                     IsTom = e.Employee1 == "Tom" ? "Yes" : "No"
                 };

        //  指定类型形式
        var q5 = from e in db.Employees
                 select new Employee
                 {
                     ID = e.ID,
                     Employee1 = e.Employee1
                 };

        //  shaped形式(整形类型)
        //  说明：其select操作使用了匿名对象，而这个匿名对象中，其属性也是个匿名对象。
        var q6 = from e in db.Employees
                 select new
                 {
                     e.Employee1,
                     EmployeeInfo = new { e.ID, e.Employee1 }
                 };

        //  嵌套
        var q7 = from e in db.Employees
                 select new
                 {
                     e.ID,
                     em = from t in db.Tasks
                          where (t.EmployeeID == 2)
                          select t,
                     e.Employee1
                 };

        //  Distinct
        var q8 = (from t in db.Tasks
                 select t.EmployeeID).Distinct();

        GridView1.DataSource = q8;
        GridView1.DataBind();
    }
    private void Label1_Bind()
    { 
        string[] names = { "Burke", "Connor", "Frank", 
                       "Everett", "Albert", "George", 
                       "Harris", "David" };

        IEnumerable expr = from s in names
                           where s.Length == 5
                           orderby s
                           select s.ToUpper();

        string str = string.Empty;
        foreach(string item in expr)
        {
            str += item + "<br>";
        }
        Label1.Text = str;
    }
}
